/***************************************************************************
 *   Copyright (C) 2008 by Ryszard Krol,   *
 *   rysiek.krol@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "rexifdata.h"
#include <cassert>


/**
* Konstruktor Klasy
*/
RExifData::RExifData ( char* filePath )
{
        try {
                Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open ( filePath );
                assert ( image.get() != 0 );
                image->readMetadata();
                this->exifData = image->exifData();
                if ( !this->exifData.empty() ) {
                        this->extractThumbnail();
                        this->extractDateTime();
                        this->isNull = false;
                } else {
                        this->isNull = true;
                }
        } catch (Exiv2::AnyError& error) {
                this->isNull = true;
        }
}

/**
* Konstruktor Klasy
*/
RExifData::RExifData ( QString *filePath )
{
	RExifData ( filePath->toLatin1().data() );
}

/**
* Destruktor Klasy
*/
RExifData::~RExifData()
{

}

/**
* Zwraca true, jeśli w pliku są dane EXIF, lub false, gdy takich danych nie ma
*/
bool RExifData::isExif()
{
	if (this->isNull==true) return false;
	return !this->exifData.empty();
}

/**
* Wyciąga date zrobienia zdjęcia z danych EXIF
*/
void RExifData::extractDateTime()
{
        Exiv2::ExifKey* klucz = '\0';
        if (this->isKeyExist("Exif.Photo.DateTimeOriginal")) klucz = new Exiv2::ExifKey ( "Exif.Photo.DateTimeOriginal" );
        if (this->isKeyExist("Exif.Image.DateTime")) klucz = new Exiv2::ExifKey ( "Exif.Image.DateTime" );
        if (klucz!='\0') {
                Exiv2::ExifData::iterator itsIterator = this->exifData.findKey ( *klucz );
                this->date = QDateTime::fromString ( QString::fromStdString ( itsIterator->value().toString() ), "yyyy:MM:dd hh:mm:ss" );
                delete klucz;
        }
}

/**
* Wyciąga miniaturke z danych EXIF
*/
void RExifData::extractThumbnail()
{
	std::pair< uchar *, long > data;
	Exiv2::DataBuf  buffer = this->exifData.copyThumbnail();
	data = buffer.release();
	this->thumbnail.loadFromData ( data.first, data.second ); 
	delete data.first;
}

/**
* Zwraca date zrobienia zdjęcia
*/
QDateTime RExifData::getDate()
{
	return this->date;
}

/**
* Zwraca miniaturke zdjęcia
*/
QImage RExifData::getThumbnail()
{
	return this->thumbnail;
}

/**
* Sprawdza, czy dany klucz istnieje w danych EXIF pliku
*/
bool RExifData::isKeyExist(std::string klucz){
	Exiv2::ExifData::const_iterator end = exifData.end();
	for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {

		if(i->key()==klucz) return true;
	}
	return false;
}
